/*
----------------------------------------------------------------------------
Copyright (c) 2007 - 2009, Wangxing SHI	<swxlion@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above
	  copyright notice, this list of conditions and the
	  following disclaimer.

    * Redistributions in binary form must reproduce the above
	  copyright notice, this list of conditions and the following
	  disclaimer in the documentation and/or other materials
	  provided with the distribution.

    * Neither the copyright holder's name nor the names of its
	  contributors may be used to endorse or promote products
	  derived from this software without specific prior written
	  permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
*/

/*--------------------------- TBasalPool.hpp ------------------------------*/
/*===========================================================================
FILE: TBasalPool.hpp
Author: Wangxing SHI (swxlion)
Version: 0.0.1
Date: 2009-06-28

File Description:
    Define the basic pool for Luna Log.

Dependencies:
    Platform: Platform Independent.

History:
    version        author        date       description
	----------------------------------------------------
	0.0.1          swxlion       09-06-28   Created the first version.
===========================================================================*/
#ifndef _LUNA_LOG__TBASAL_POOL_H_
#define _LUNA_LOG__TBASAL_POOL_H_

/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#ifdef _MSC_VER
  #include "inc/WindowsPlatform.inc"
  #include <windows.h>
#else
  #include <pthread.h>
#endif
#include "TBasalPoolMap.hpp"
#include "MacroDefine.hpp"
/*===============================================================================
  CLASS & STRUCTURE DEFINITIONS
=============================================================================== */
namespace LunaLog
{
	template <typename PointerType>
	class TBasalPool
	{
	private:
		TBasalPoolMap<PointerType>		Collector;
		bool							bCriticalInited;

#ifdef _MSC_VER
		CRITICAL_SECTION		Critical;
#else
		pthread_mutex_t			Mutex;
#endif

		bool		InitCritical();
		void		FreezeAll() {};		//-- Required Specialization.
		void		Free();				//-- Required Specialization.

	public:
		PointerType	Get(const char        *);
		PointerType	Get(const std::string *);
		PointerType	Get(const std::string &);

		PointerType	UnLockedGet(const char        *);
		PointerType	UnLockedGet(const std::string *);
		PointerType	UnLockedGet(const std::string &);

		bool		Add(const char        *, PointerType);
		bool		Add(const std::string *, PointerType);
		bool		Add(const std::string &, PointerType);
		void		Remove(const char        *);
		void		Remove(const std::string *);
		void		Remove(const std::string &);

		bool		IsInited() { return bCriticalInited; }
		bool		Init() { return InitCritical(); }

		TBasalPool();
		virtual	~TBasalPool();
	};

	/*===============================================================================
	INLINE FUNCTION DEFINITIONS: Basal Pool Functions: Class TBasalPool
	=============================================================================== */
	template <typename PointerType>
	bool TBasalPool<PointerType>::InitCritical()
	{
#ifdef _MSC_VER
#include "inc/InitCritical.windows.inc"
#else
#include "inc/InitCritical.POSIX.inc"
#endif
	}

	//------------------[ Public functions: UnLockedGet ]--------------------------
	template <typename PointerType>
	PointerType TBasalPool<PointerType>::UnLockedGet(const char *name)
	{
		TBasalPoolMap<PointerType>::iterator	Iter = Collector.find(name);
		return ((Iter == NULL) ? 0 : Iter->content);
	}

	template <typename PointerType>
	PointerType TBasalPool<PointerType>::UnLockedGet(const std::string *name)
	{
		TBasalPoolMap<PointerType>::iterator	Iter = Collector.find(name);
		return ((Iter == NULL) ? 0 : Iter->content);
	}

	template <typename PointerType>
	PointerType TBasalPool<PointerType>::UnLockedGet(const std::string &name)
	{
		TBasalPoolMap<PointerType>::iterator	Iter = Collector.find(name);
		return ((Iter == NULL) ? 0 : Iter->content);
	}
	//---------------------[ Public functions: Get ]--------------------------------
	template <typename PointerType>
	PointerType TBasalPool<PointerType>::Get(const char *name)
	{
		if(!bCriticalInited)
			return 0;

		LUNALOG__GET_LOCK
		TBasalPoolMap<PointerType>::iterator	Iter = Collector.find(name);
		LUNALOG__RELEASE_LOCK
		return ((Iter == NULL) ? 0 : Iter->content);
	}

	template <typename PointerType>
	PointerType TBasalPool<PointerType>::Get(const std::string *name)
	{
		if(!bCriticalInited)
			return 0;

		LUNALOG__GET_LOCK
		TBasalPoolMap<PointerType>::iterator	Iter = Collector.find(name);
		LUNALOG__RELEASE_LOCK
		return ((Iter == NULL) ? 0 : Iter->content);
	}

	template <typename PointerType>
	PointerType TBasalPool<PointerType>::Get(const std::string &name)
	{
		if(!bCriticalInited)
			return 0;

		LUNALOG__GET_LOCK
		TBasalPoolMap<PointerType>::iterator	Iter = Collector.find(name);
		LUNALOG__RELEASE_LOCK
		return ((Iter == NULL) ? 0 : Iter->content);
	}

	//---------------------[ Public functions: Add ]--------------------------------
	template <typename PointerType>
	bool TBasalPool<PointerType>::Add(const char *name, PointerType pointer)
	{
		bool	bRev = false;

		if(!bCriticalInited)
			return bRev;

		std::string		strName = name;
		LUNALOG__GET_LOCK
		bRev = Collector.insert(strName, pointer);
		LUNALOG__RELEASE_LOCK
		return bRev;
	}

	template <typename PointerType>
	bool TBasalPool<PointerType>::Add(const std::string *name, PointerType pointer)
	{
		bool	bRev = false;

		if(!bCriticalInited)
			return bRev;
		LUNALOG__GET_LOCK
		bRev = Collector.insert(*name, pointer);
		LUNALOG__RELEASE_LOCK
		return bRev;
	}

	template <typename PointerType>
	bool TBasalPool<PointerType>::Add(const std::string &name, PointerType pointer)
	{
		bool	bRev = false;

		if(!bCriticalInited)
			return bRev;

		LUNALOG__GET_LOCK
		bRev = Collector.insert(name, pointer);
		LUNALOG__RELEASE_LOCK
		return bRev;
	}

	//---------------------[ Public functions: Remove ]--------------------------------
	template <typename PointerType>
	void TBasalPool<PointerType>::Remove(const char * name)
	{
		if(!bCriticalInited)
			return;

		LUNALOG__GET_LOCK
		bRev = Collector.erase(name);
		LUNALOG__RELEASE_LOCK
	}

	template <typename PointerType>
	void TBasalPool<PointerType>::Remove(const std::string * name)
	{
		if(!bCriticalInited)
			return;

		LUNALOG__GET_LOCK
		bRev = Collector.erase(name);
		LUNALOG__RELEASE_LOCK
	}

	template <typename PointerType>
	void TBasalPool<PointerType>::Remove(const std::string & name)
	{
		if(!bCriticalInited)
			return;

		LUNALOG__GET_LOCK
		bRev = Collector.erase(name);
		LUNALOG__RELEASE_LOCK
	}

	//---------------------[ Public functions: Free ]--------------------------------
	template <typename PointerType>
	void TBasalPool<PointerType>::Free()
	{	
		LUNALOG__GET_LOCK
		FreezeAll();			
		Collector.clear();
		LUNALOG__RELEASE_LOCK
	}

	//-----------------[ Constructure functions ]------------------------------
	template <typename PointerType>
	TBasalPool<PointerType>::TBasalPool(): Collector(), bCriticalInited(false)
	{
		InitCritical();
	}

	template <typename PointerType>
	TBasalPool<PointerType>::~TBasalPool()
	{
		Free();
		if( bCriticalInited == true )
		{
			LUNALOG__DELETE_LOCK
		}
	}
}

#endif // _LUNA_LOG__TBASAL_POOL_H_
